﻿using System;
using System.Linq;
using BO.ProjectManager;
using BO.Infrastructure.Utils;
using GUI.Extensions;

namespace GUI.MasterPages
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //ltrBase.Text = string.Format("<base href=\"{0}/\" />", AppSettings.Domain.TrimEnd('/'));
            var cssAndJsConfig = PMBO.GetExtensionSourceSets();
            if (cssAndJsConfig != null && cssAndJsConfig.Any())
            {
                var extensionSet =
                    cssAndJsConfig.FirstOrDefault(
                        s => s.Url.Equals(Request.Url.AbsolutePath, StringComparison.InvariantCultureIgnoreCase));
                if (extensionSet == null)
                {
                    extensionSet =
                    cssAndJsConfig.FirstOrDefault(
                        s => Request.Url.AbsolutePath.Contains(s.Url));
                }
                if (extensionSet != null)
                {
                    if (extensionSet.CssSourceSet.Length > 0)
                    {
                        foreach (var css in extensionSet.CssSourceSet)
                        {
                            PresentationUtils.AddCss(Page, css);
                            
                        }
                    }

                    if (extensionSet.JsSourceSet.Length > 0)
                    {
                        foreach (var js in extensionSet.JsSourceSet)
                        {
                            PresentationUtils.AddJs(Page, js);
                        }
                    }
                }
            }


            if (RequestExtensions.IsHomePage)
            {
                var ctrKenh14 = LoadControl("/Controls/Embed/Kenh14Embed.ascx");
                if (ctrKenh14 != null)
                {
                    phKenh14Embed.Controls.Add(ctrKenh14);
                }
            }
        }
    }
}